package com.zhima.auto;

import com.zhima.properties.ZhimaMinioProperties;
import com.zhima.service.ZhimaMinioService;
import com.zhima.service.impl.ZhimaMinioServiceImpl;
import io.minio.MinioClient;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;

/**
 * @author : 芝麻
 * @date : 2023-02-10 09:06
 **/
@ConditionalOnClass(name = {"io.minio.MinioClient", "okhttp3.OkHttpClient"})
@EnableConfigurationProperties(ZhimaMinioProperties.class)
public class ZhimaMinioAutoConfiguration {

    @Bean
    public MinioClient minioClient(ZhimaMinioProperties minioProperties) {
        return MinioClient.builder()
                .endpoint(minioProperties.getEndpoint())
                .credentials(minioProperties.getAccessKey(), minioProperties.getSecretKey())
                .build();
    }

    @Bean
    @ConditionalOnMissingBean(ZhimaMinioService.class)
    public ZhimaMinioService minioService() {
        return new ZhimaMinioServiceImpl();
    }
}
